package pl.ydp.components.mathcell.mathrecognizer.geometry
{
	import flash.geom.Point;
	
	public class Angle
	{
		
		public static function mean(a1:Number, a2:Number):Number{
			var result:Number;
			if (Math.abs(a1-a2) > 180){
				
				result = (a1+a2+360)/2;
				
				if (result > 360){
					return result - 360;
				} else {
					return result;
				}
				
			}
			
			return (a1+a2)/2;
			
		}
		
		//---------------------------------------------------------------------
		
		public static function meanAlpha(a1:Number, a2:Number, alpha:Number):Number{
			var result:Number;
			if (Math.abs(a1 - a2) > 180){
				if (a1 > 180){
					result =  a1*alpha + (a2+360)*(1-alpha);					
				} else {
					result =  (a1+360)*alpha + a2*(1-alpha);					
				}
				
				if (result > 360){
					result -= 360;
				}
				
				return result;
				
			} 
			
			return a1*alpha + a2*(1-alpha);
			
		}
		
		//---------------------------------------------------------------------
		
		public static function add(a1:Number, a2:Number):Number{
			if (a1 + a2 > 360){
				return  a1+a2-360;
			} 
			
			return a1+a2;
			
		}
		
		//---------------------------------------------------------------------
		
		public static function subtract(a1:Number, a2:Number):Number{
			if (a1 < a2){
				return  a1-a2+360;
			} 
			
			return a1-a2;
			
		}
		
		//---------------------------------------------------------------------
		
		public static function difference(a1:Number, a2:Number):Number{
			
			var result:Number = a2 - a1;
			
			if (result > 180){
				result -= 360;
			} else if (result < -180){
				result += 360;
			}
			
			return result;
			
		}
		
		
		//---------------------------------------------------------------------
		
		public static function differencePure(a1:Number, a2:Number):Number{
			
			var result:Number;
			if (a1 < a2){
				result = 360 - (a1 - a2);
			} else {
				result = a2 - a1;
			} 
			
			return result;
			
		}
				
		//---------------------------------------------------------------------
		
		public static function differenceAbs(a1:Number, a2:Number):Number{
			var result:Number;
			
			result = a1-a2;
			
			result = Math.abs(result);
			
			if (result < 180){
				return result;
			} else {
				return 360-result;
			}
			
		}
		
		//---------------------------------------------------------------------
		
		
		public static function differenceAbsUnified(a1:Number, a2:Number):Number{
			var result:Number;
			
			result = a1-a2;
			
			result = Math.abs(result);
			
			if (result < 90){
				return result;
			} else {
				return 180-result;
			}
			
		}
		
		//---------------------------------------------------------------------
		
		public static function differenceDirectionAngleAbs(a1:Number, a2:Number):Number{
			var result:Number;
			
			result = a1-a2;
			
			result = Math.abs(result);
			
			if (result < 180 - 45/2){
				return result;
			} else {
				return 180-result;
			}
			
		}
			
		//---------------------------------------------------------------------	
		
		public static function differenceAbs90(a1:Number, a2:Number):Number{
			var a:Number = differenceAbs(a1, a2);
			
			if (a > 90){
				a = 180 - a;
			}
			
			return a;
			
		}

		
		//---------------------------------------------------------------------
		
		public static function angle2directionAngle(a:Number):Number{
			
			if (a > 180 - 45/2){
				a -= 180;
			}
			if (a > 180 - 45/2){
				a -= 180;
			}
			if (a < - 45/2){
				a += 180;
			}
			
			return a;
			
		}
		
		//---------------------------------------------------------------------
		
		public static function points2degrees(p1:Point, p2:Point):Number{
			var result:Number = Math.atan2(p2.y-p1.y, p2.x-p1.x)*180/Math.PI;
			if (result < 0){
				result += 360;
			}
			return result;
		}
		
		//---------------------------------------------------------------------
		
		public static function vertexAngle(base:Point, p1:Point, p2:Point):Number{
			return differenceAbs( points2degrees(base, p1) , points2degrees(base, p2) );
		}
		
		//---------------------------------------------------------------------
		
		public static function deg2rad(degrees:Number):Number{
			return degrees*Math.PI/180;
		}
		//---------------------------------------------------------------------
		
		public static function rad2deg(degrees:Number):Number{
			return degrees/Math.PI*180;
		}
	}
}